package com.bee.beemanage.api.controller;

import com.bee.beemanage.api.entity.SysRoleMenu;
import com.bee.beemanage.api.entity.input.IdList;
import com.bee.beemanage.api.service.SysRoleMenuService;
import com.bee.beemanage.system.common.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.bee.beemanage.system.common.utils.result.ResultTool.success;

@RestController
@RequestMapping("/bee-manage/sys-role-menu")
@Api(value ="角色菜单关联表",tags = "角色菜单关联表")
public class SysRoleMenuController {

    @Resource
    private SysRoleMenuService sysRoleMenuService;

    /**
     * 批量编辑角色菜单关联
     * @param idList 角色 角色菜单数组
     * @return 编辑是否成功
     */
    @ApiOperation(value = "批量编辑角色菜单关联", notes = "批量编辑角色菜单关联")
    @PostMapping("/editBatch")
    @PreAuthorize("hasAnyAuthority('sys:roleMenu:editBatch')")
    public Result editBatch(@RequestBody IdList idList) {
        return sysRoleMenuService.editBatch(idList.getId(),idList.getListString());
    }

    /**
     * 通过roleId查询所有角色菜单关联
     * @return 角色菜单信息
     */
    @ApiOperation(value = "通过roleId查询所有角色权限关联", notes = "通过roleId查询所有角色菜单关联")
    @GetMapping("/{roleId}")
    @PreAuthorize("hasAnyAuthority('sys:roleMenu:getAllByRole')")
    public Result getAllByRole(@PathVariable String roleId) {
        //获取菜单树
        List<SysRoleMenu> roleMenus = sysRoleMenuService.getRoleMenus(roleId);
        return success(roleMenus);
    }
}